﻿using System;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace Ns.Data.Entity
{
    /// <summary>
    /// Represents a type supporting change notification.
    /// </summary>
#if !SILVERLIGHT
    [Serializable]
#endif
    [DataContract]
    public abstract class NotifyEntity : INotifyPropertyChanged
    {
        /// <summary>
        /// Occurs after the value of a property is changed.
        /// </summary>
        public virtual event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raises the <see cref="PropertyChanged"/> event.
        /// </summary>
        /// <param name="propertyName">The name of the property whose value has changed.</param>
        protected virtual void OnPropertyChanged(string propertyName)
        {
            this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }

        /// <summary>
        /// Raises the <see cref="PropertyChanged"/> event.
        /// </summary>
        /// <param name="e">A <see cref="PropertyChangedEventArgs"/> that contains the event data.</param>
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            //keep handler to avoid race condition
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, e);
        }
    }
}
